﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfSnqkGasAnalysis.IBusiness_impl;
using WpfSnqkGasAnalysis.Model;

namespace WpfSnqkGasAnalysis
{
    /// <summary>
    /// PageCleanQi.xaml 的交互逻辑
    /// </summary>
    public partial class PageCleanQi : Page
    {
        public PageCleanQi()
        {
            InitializeComponent();
        }
        CleanGasBusiness business = new CleanGasBusiness();

        //初始化页面数据 //创建时间：2022-12-13 22:42:13。作者：王浩力
        private void Page_Loaded(object sender, RoutedEventArgs e)
        {
            LoadPageData(1);

            //步骤查询条件  //创建时间：2023-1-16 14:44:50
            cmb_Step.ItemsSource = CommonConfig.StepList;
            cmb_Step.DisplayMemberPath = "Value";
            cmb_Step.SelectedValuePath = "Key";
        }

        //编辑
        private void btnEdit_Click(object sender, RoutedEventArgs e)
        {
            //选中的数据
            Clean_gas? gas = this.dataGrid.SelectedItem as Clean_gas;

            WindowCleanGasEdit windowCleanGasEdit = new WindowCleanGasEdit();
            windowCleanGasEdit.clean_Gas = gas;
            windowCleanGasEdit.Closing += Input_Closing;
            windowCleanGasEdit.ShowDialog();
        }

        //打印
        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {
            //选中的数据
            Clean_gas? gas = this.dataGrid.SelectedItem as Clean_gas;

            //查询完整数据
            CleanGasBusiness busi = new CleanGasBusiness();
            var model = busi.GetCmboxStr(gas.Id);

            //打印备注
            model.source_Dicts = new SourceDictBusiness().GetListByGroup("clean_bz_remark_list", 1);

            WindowProofreadPrintPreview ppv = new WindowProofreadPrintPreview("FlowDocumentCleanPrint.xaml", model, new PrintGas_analysisDocument());
            //ppv.Owner = this.Content;
            ppv.ShowInTaskbar = false;
            ppv.Show();
        }

        //日志
        private void Button_Click_5(object sender, RoutedEventArgs e)
        {
            //选中的数据
            Clean_gas? gas = this.dataGrid.SelectedItem as Clean_gas;

            WindowCleanStepLog windowCleanStepLog = new WindowCleanStepLog();
            windowCleanStepLog.clean_Gas = gas;
            windowCleanStepLog.ShowDialog();
        }

        //校对
        private void Button_Click_3(object sender, RoutedEventArgs e)
        {
            //选中的数据
            Clean_gas? gas = this.dataGrid.SelectedItem as Clean_gas;

            WindowCleanGasProofread windowCleanGasProofread = new WindowCleanGasProofread();
            windowCleanGasProofread.clean_Gas = gas;
            windowCleanGasProofread.Closing += Input_Closing;
            windowCleanGasProofread.ShowDialog();
        }

        //审核  //创建时间：2022-12-14 22:59:08。
        private void Button_Click_4(object sender, RoutedEventArgs e)
        {
            //选中的数据
            Clean_gas? gas = this.dataGrid.SelectedItem as Clean_gas;

            WindowCleanCheck cleanCheck = new WindowCleanCheck();
            cleanCheck.clean_Gas = gas;
            cleanCheck.Closing += Input_Closing;
            cleanCheck.ShowDialog();
        }

        //删除
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            //选中的数据
            Clean_gas? gas = this.dataGrid.SelectedItem as Clean_gas;

            var result = MessageBox.Show($"确认删除样品编号为{gas.Sample_no}的记录吗？", "删除确认", MessageBoxButton.YesNo);
            if (result == MessageBoxResult.Yes)
            {
                var op = business.Delete(gas.Id);
                if (op.Code == (int)ResultCode.Success)
                {
                    LoadPageData(pager.PageIndex);
                }
                ShowAlertHelpter.ShowAlert(op.Msg);
            }
        }

        //录入
        private void input_Button_Click_1(object sender, RoutedEventArgs e)
        {
            WindowCleanInput input = new WindowCleanInput();
            input.Closing += Input_Closing;
            input.ShowDialog();
        }

        //刷新 分页数据
        private void Input_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            LoadPageData(pager.PageIndex);
        }

        //查看报告
        private void see_Button_Click_1(object sender, RoutedEventArgs e)
        {

        }

        //查询   //创建时间：2022-12-14 11:04:04。作者：王浩力
        private void search_Button_Click(object sender, RoutedEventArgs e)
        {
            LoadPageData(1);
        }

        /// <summary>
        /// 加载分页数据
        /// </summary>
        /// <param name="p"></param>
        private void LoadPageData(int p)
        {
            var where = new Clean_gas();
            where.Sample_no = txtSampleNo.Text.Trim();

            //步骤值查询， //创建时间：2023-1-16 16:43:21。
            if (cmb_Step.SelectedValue != null && cmb_Step.SelectedValue?.ToString() != "-1")
            {
                where.O_step = Convert.ToByte(cmb_Step.SelectedValue);
            }

            var page = business.GetPage(where, p, CommonConfig.PageSize);
            dataGrid.ItemsSource = page.List;

            pager.PageIndex = p;
            pager.PageSize = page.PageSize;
            pager.Total = page.Total;
        }

        //点击页码 。触发
        private void PageBar_PageIndexChanged(object sender, RoutedPropertyChangedEventArgs<int> e)
        {
            LoadPageData(e.NewValue);
        }
    }
}
